package com.ln.lnzw.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.adapter.MyConsultationAdapter;
import com.ln.lnzw.adapter.SuggestionAdapter;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.MyConsultationBean;
import com.ln.lnzw.bean.SuggestBean;
import com.ln.lnzw.net.HttpMethods;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 : wlt
 * 创建时间:  2018-06-13
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:
 * In me the tiger sniffs the rose
 */

public class MyConsultationActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.bianji)
    TextView bianji;
    @BindView(R.id.rv_recyclerview)
    RecyclerView rvRecyclerview;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.tv_cancle)
    TextView tvCancle;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private MyConsultationAdapter mAdapter;
    private List<MyConsultationBean.ResultBean.ListBean> mList = new ArrayList<>();
    private String name;

    private String userId = "";
    private String token = "";
    private String creditID = "";
    private int page = 1;
    private int limit = 10;
    private int lastSize = 0;
    private int nowSize = 0;
    private int delayMillis = 1000;
    private static final String TAG = "MyConsultationActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluation_suggestion_complain);
        ButterKnife.bind(this);
        setView();
        initData();
    }

    private void initData() {
        userId = activity.spUtils.getString(AppConstant.USER_UID,"");
        token = activity.spUtils.getString(AppConstant.TOKEN,"");
        creditID = activity.spUtils.getString(AppConstant.USER_CREDITID,"");
//        Log.e(TAG, "建议参数 "+userId+"__"+token+"___"+creditID );
        HttpMethods.getInstanceCenter().info.getMyConsultaiton(token,userId,page, limit,creditID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MyConsultationBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(MyConsultationBean suggestBean) {
                        Log.e(TAG, "咨询 "+suggestBean.toString() );
                        if (!mList.isEmpty()){mList.clear();}
                        if (null!= suggestBean.getResult()&& null != suggestBean.getResult().getList()){
                            mList.addAll(suggestBean.getResult().getList());
                            lastSize = mList.size();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "获取我的咨询列表失败 "+e.toString() );
                        dispose();
                    }

                    @Override
                    public void onComplete() {
                        mAdapter.setNewData(mList);
                        mAdapter.notifyDataSetChanged();
                    }
                });


    }

    private void setView() {
        name = getIntent().getStringExtra("name");
        tvName.setText(name);
        rvRecyclerview.setLayoutManager(new LinearLayoutManager(activity));
        mAdapter = new MyConsultationAdapter(R.layout.item_activity_evaluation_suggestion_complain, mList);
        rvRecyclerview.setAdapter(mAdapter);

        ViewGroup.LayoutParams paramsCancle = tvCancle.getLayoutParams();

        paramsCancle.width = ScreenUtils.getScreenWidth()*28/75;
        paramsCancle.height = paramsCancle.width*2/7;
        tvCancle.setLayoutParams(paramsCancle);

        ViewGroup.LayoutParams paramsDelete = tvDelete.getLayoutParams();

        paramsDelete.width = ScreenUtils.getScreenWidth()*28/75;
        paramsDelete.height = paramsDelete.width*2/7;
        tvDelete.setLayoutParams(paramsDelete);

        refreshLayout.setDisableContentWhenRefresh(true);//是否在刷新的时候禁止列表的操作
        refreshLayout.setDisableContentWhenLoading(true);//是否在加载的时候禁止列表的操作
        //设置下拉刷新监听事件
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                downRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                onLoadMoreRequested();
            }
        });
    }

    /**
     * 下拉刷新方法,重置页数重新请求
     */
    private void downRefresh(){
        page = 1;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setNoMoreData(false);
                initData();
                refreshLayout.finishRefresh();
            }
        }, delayMillis);
    }

    /**
     * 上拉加载更多方法
     */
    private void onLoadMoreRequested(){
        userId = activity.spUtils.getString(AppConstant.USER_UID,"");
        token = activity.spUtils.getString(AppConstant.TOKEN,"");
        creditID = activity.spUtils.getString(AppConstant.USER_CREDITID,"");
        HttpMethods.getInstanceCenter().info.getMyConsultaiton(token,userId,page+1, limit,creditID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MyConsultationBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(MyConsultationBean suggestBean) {

                        if (null!= suggestBean.getResult()&& null != suggestBean.getResult().getList()){
                            mList.addAll(suggestBean.getResult().getList());
                            nowSize = mList.size();
                            page = page+1;
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "获取我的建议下一页失败 "+e.toString() );
                        dispose();
                    }

                    @Override
                    public void onComplete() {
                        if (nowSize>lastSize){
                            mAdapter.setNewData(mList);
                            mAdapter.notifyDataSetChanged();
                        }
                        refreshLayout.finishLoadMore(delayMillis);
                    }
                });
    }

    @OnClick({R.id.iv_back, R.id.bianji})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bianji:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        dispose();
    }
}
